# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

CXX= g++ -g
C_OBJS= Client.o
S_OBJS= Server.o ClientHandler.o ThreadPool.o UserMessages.o
SOBJS = $(S_OBJS)
COBJS = $(C_OBJS)

LIBS = -lpthread -lrt

CCFLAGS= -g

all: 	build-server build-client

test:   $(TOBJS)
	$(CXX) $(CCFLAGS) -o test $(TOBJS) $(LIBS)
	
build-server:	$(S_OBJS)
	$(CXX) $(CCFLAGS) -o Server $(S_OBJS) $(LIBS)

run-server:	build-server
	./Server

build-client:	$(C_OBJS)
	$(CXX) $(CCFLAGS) -o Client $(C_OBJS) $(LIBS)

run-client:	build-client
	./Client


val:	$(S_OBJS)
	valgrind -v --leak-check=full --show-reachable=yes ./Server

pull:	
	hg pull
	hg update -C

clean:
	rm -f $(SOBJS) $(SOBJS:.o=.d)
	rm -f $(COBJS) $(COBJS:.o=.d)

realclean:
	rm -f $(COBJS) $(COBJS:.o=.d) $(SOBJS) $(SOBJS:.o=.d) Client Server


# These lines ensure that dependencies are handled automatically.
%.d:	%.cpp
	$(SHELL) -ec '$(CPP) -M $(CPPFLAGS) $< \
		| sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
		[ -s $@ ] || rm -f $@'

%.d:	%.cc
	$(SHELL) -ec '$(CC) -M $(CPPFLAGS) $< \
		| sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
		[ -s $@ ] || rm -f $@'
include	$(SOBJS:.o=.d) $(COBJS:.o=.d)